﻿using CandidateManager.Areas.Coach.Models;
using System;
using System.Linq;
using System.Web.Mvc;
using WI.CandidateManager.Business.Objects;
using CommonEntities = WI.CandidateManager.Common.Data.Entities;
using WI.CandidateManager.Common.Data.Entities;

namespace CandidateManager.Areas.Coach.Controllers
{
    public class SubjectsController : CandidateManager.Controllers.AutorizeBaseController
    {
        private SubjectBO subjectBO;

        public SubjectsController()
            : base()
        {
            subjectBO = new SubjectBO();
        }

        [Authorize]
        public ActionResult Delete(long subjectId)
        {
            if (subjectBO.DeleteSubject(subjectId))
            {
                return RedirectToAction("Manage", "Subjects");
            }
            else
            {
                return RedirectToAction("Manage", "Subjects");
            }
        }

        [HttpGet]
        public ActionResult Manage(long? subjectId = null)
        {
            SubjectModifyModel subject = new SubjectModifyModel()
            {
                Subjects = new CoachBO().GetSubjects(),
            };

            if (Request.IsAjaxRequest() && subjectId.HasValue)
            {
                subject.Subject = subjectBO.GetSubjects(subjectId).Single();
                return PartialView("SubjectManagement", subject.Subject);

            }
            else
            {
                return View(subject);
            }
        }

        [Authorize]
        [HttpPost]
        public ActionResult Manage(Subject subject)
        {
            if (ModelState.IsValid)
            {
                if (subjectBO.SaveSubject(subject))
                {
                    return RedirectToAction("Manage");
                }
                else
                {
                    return RedirectToAction("Manage");
                }
            }
            return RedirectToAction("Manage");
        }
    }
}
